<template>
	<div class="wrapper">
		<div class="back-icon" @click="goBack()">
			<i class="fa fa-arrow-left"></i>
		</div>
		<!-- header部分 -->
		<header>
			<p>修改密码</p>
		</header>
		<!-- 表单部分 -->
		<ul class="form-box">
			<li>
				<div class="title">
					原密码：
				</div>
				<div class="content">
					<input type="password" @blur="checkPassword()" v-model="testPassword" placeholder="请输入原来的密码">
				</div>
			</li>
			<li>
				<div class="title">
					新密码：
				</div>
				<div class="content">
					<input type="password" v-model="newPassword" placeholder="请输入新密码">
				</div>
			</li>
			<li>
				<div class="title">
					确认新密码：
				</div>
				<div class="content">
					<input type="password" v-model="confirmPassword" placeholder="请再次输入新密码">
				</div>
			</li>
		</ul>
		<div class="button-add">
			<button @click="editUserInfo()">更新</button>
		</div>
		<!-- 底部菜单部分 -->
		<Footer></Footer>
	</div>
</template>

<script>
	import Footer from '../components/Footer.vue'
	import {useRouter} from 'vue-router'
	import {useRoute} from 'vue-router'
	import {ref,onMounted,getCurrentInstance, reactive} from 'vue';
	
	export default{
		name: 'EditUserInfo',
		setup() {
			const currentInstance = getCurrentInstance();
			const router = useRouter();
			const route = useRoute();
			const user = ref({});
			const testPassword = ref('');
			const newPassword = ref('');
			const confirmPassword = ref('');
			const goBack = () => {
				router.go(-1);
			};
			const checkPassword = () => {
				if(testPassword.value!=user.value.password){
					testPassword.value='';
					swal("密码输入错误！");
				}
			}
			const editUserInfo = () => {
				if (newPassword.value != confirmPassword.value) {
					swal('前后两次输入的密码不一致！');
					return;
				}
				user.value.password=confirmPassword.value;
				currentInstance.appContext.config.globalProperties.$axios.post(
						'UserController/updateUserPass', currentInstance.appContext.config
						.globalProperties.$qs.stringify(
							user.value
						))
					.then(response => {
						currentInstance.appContext.config.globalProperties.$setSessionStorage('user',user.value);
						if (response.data > 0) {
							router.push({
								name: 'PersonalCenter'
							});
						} else {
							swal('修改密码失败！');
						}
					})
					.catch(error => {
						console.error(error);
					});
			}
			
			onMounted(() => {
				user.value = currentInstance.appContext.config.globalProperties.$getSessionStorage('user');
			})
			return{
				user,testPassword,newPassword,confirmPassword,
				editUserInfo,goBack,checkPassword
			}
		},
		components: {
			Footer
		},
	}
</script>

<style scoped>
	/*************** 总容器 ***************/
	.wrapper {
		width: 100%;
		height: 100%;
	}
	/*********** 回退按钮 ***********/
	.wrapper .back-icon{
		width: 12vw;
		height: 12vw;
		background-color: #0097FF;
		color: #FFFFFF;
		font-size: 4.8vw;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 1001;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	/*************** header ***************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		display: flex;
		justify-content: space-around;
		align-items: center;
		color: #fff;
		font-size: 4.8vw;
		position: fixed;
		left: 0;
		top: 0;
		/*保证在最上层*/
		z-index: 1000;
	}

	/*************** （表单信息） ***************/
	.wrapper .form-box {
		width: 100%;
		margin-top: 12vw;
	}

	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0vw 3vw;
		display: flex;
	}

	.wrapper .form-box li .title {
		flex: 0 0 18vw;
		font-size: 3vw;
		font-weight: 700;
		color: #666;
	}

	.wrapper .form-box li .content {
		flex: 1;
		display: flex;
		align-items: center;
	}

	.wrapper .form-box li .content input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 3vw;
	}

	.wrapper .button-add {
		box-sizing: border-box;
		padding: 4vw 3vw 0vw 3vw;
	}

	.wrapper .button-add button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		border: none;
		outline: none;
		border-radius: 4px;
		color: #fff;
		background-color: #38CA73;
	}
</style>